package logic

import (
	"context"
	"fmt"
	"week1/goodrpc/goodrpc"
	"week1/goodrpc/internal/svc"
	"week1/models/mysql"
	"week1/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthUserLogic {
	return &AuthUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AuthUserLogic) AuthUser(in *goodrpc.AuthUserRequest) (*goodrpc.AuthUserResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User
	data, err := findUser.FindUserById(in.UserId)
	if err != nil {
		return nil, err
	}
	if data.ID == 0 {
		return nil, fmt.Errorf("该用户不存在")
	}
	if data.RealName != "" {
		return nil, fmt.Errorf("已被实名")
	}
	err = pkg.Authorization(in.IdCard, in.RealName)
	if err != nil {
		return nil, err
	}

	err = findUser.AuthAdd()
	if err != nil {
		return nil, fmt.Errorf("实名添加失败")
	}

	return &goodrpc.AuthUserResponse{
		UserId: int64(findUser.ID),
	}, nil
}
